package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.constant.StatusConstant;
import com.b2.context.BaseContext;
import com.b2.mapper.TeacherMapper;
import com.b2.mapper.UserMapper;
import com.b2.mapper.VideoMapper;
import com.b2.pojo.dto.*;
import com.b2.pojo.entity.*;
import com.b2.pojo.vo.DirectorGetworksVo;
import com.b2.pojo.vo.ParentVideoInformation;
import com.b2.pojo.vo.TeacherGetworksVo;
import com.b2.pojo.vo.TeacherVideoInformation;
import com.b2.properties.AiProperties;
import com.b2.utils.AliOssUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.b2.service.TeacherVideoService;
import com.b2.mapper.TeacherVideoMapper;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author 悦神
* @description 针对表【teacher_video】的数据库操作Service实现
* @createDate 2024-05-22 10:32:11
*/
@Service
public class TeacherVideoServiceImpl extends ServiceImpl<TeacherVideoMapper, TeacherVideo>
    implements TeacherVideoService{

     @Autowired
    AliOssUtil aliOssUtil;

     @Autowired
    AiProperties aiProperties;
    @Autowired
    TeacherVideoMapper teacherVideoMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    VideoMapper videoMapper;
    @Override
    @Transactional
    public void teachercommitvideo(TeacherCommitVideoDto teacherCommitVideoDto) {
        TeacherVideo teacherVideo = new TeacherVideo();
        //通过用户id关联得出teacherid
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Teacher> o1 = new LambdaQueryWrapper<>();
        o1.eq(Teacher::getPhone,phone);
        Teacher teacher = teacherMapper.selectOne(o1);
        Long id = teacher.getId();
        //通过classid得到teacherid
        //Class aClass = classMapper.selectById(parentCommitVideoDto.getClassId());
        //Long teacherId = aClass.getTeacherId();
        //当前班级没有老师


        teacherVideo.setTeacherId(id);
        teacherVideo.setClassId(teacherCommitVideoDto.getClassId());
        teacherVideo.setVideoId(teacherCommitVideoDto.getVideoId());
        teacherVideo.setTeacherVideoUrl((teacherCommitVideoDto.getParentVideoUrl()));
        teacherVideo.setCreateTime(LocalDateTime.now());
        teacherVideo.setStatus(StatusConstant.BEFORE_CORRECTION);
        teacherVideoMapper.insert(teacherVideo);
    }

    @Override
    public TeacherVideoInformation getinformation(Long videoid, Long classid) {
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        String phone = user.getPhone();
        LambdaQueryWrapper<Teacher> o1 = new LambdaQueryWrapper<>();
        o1.eq(Teacher::getPhone,phone);
        Teacher teacher = teacherMapper.selectOne(o1);
        Long id = teacher.getId();
        //通过classid得到teacherid
        //Class aClass = classMapper.selectById(classid);
        //Long teacherId = aClass.getTeacherId();

        LambdaQueryWrapper<TeacherVideo> o2 = new LambdaQueryWrapper<>();
        o2.eq(TeacherVideo::getTeacherId,id);
        o2.eq(TeacherVideo::getVideoId,videoid);
        o2.eq(TeacherVideo::getClassId,classid);
        TeacherVideo teacherVideo = teacherVideoMapper.selectOne(o2);
        if (teacherVideo==null)
        {
            return null;
        }
        TeacherVideoInformation teacherVideoInformation = new TeacherVideoInformation();
        teacherVideoInformation .setTeacherVideoUrl(teacherVideo.getTeacherVideoUrl());
        teacherVideoInformation .setDirectorResponse(teacherVideo.getDirectorResponse());
        teacherVideoInformation .setCreateTime(teacherVideo.getCreateTime());
        teacherVideoInformation .setId(teacherVideo.getId());
        teacherVideoInformation.setOverrule(teacherVideo.getOverrule());
        teacherVideoInformation.setAiResponse(teacherVideo.getAiResponse());
        return teacherVideoInformation;
    }

    @Override
    public Integer getstatus(Long id) {
        TeacherVideo teacherVideo = teacherVideoMapper.selectById(id);
        if(teacherVideo.getStatus()==1){
           teacherVideo.setStatus(100);
           teacherVideoMapper.updateById(teacherVideo);
        }
        return teacherVideo.getStatus();
    }

    @Transactional
    @Override
    public void teacherupdatevideo(teacherUpdateVideoDto teacherUpdateVideoDto) {

        //批改后不能修改
        TeacherVideo teacherVideo1 = teacherVideoMapper.selectById(teacherUpdateVideoDto.getId());
        if (teacherVideo1.getStatus()==1) {
            throw new RuntimeException(MessageConstant.UPDATE_FAIL);
        }

        TeacherVideo teacherVideo = new TeacherVideo();

        teacherVideo.setId(teacherUpdateVideoDto.getId());

        teacherVideo.setTeacherVideoUrl(teacherUpdateVideoDto.getNewteacherVideoUrl());
        //修改后将状态变换为未批改状态;
        teacherVideo.setStatus(StatusConstant.BEFORE_CORRECTION);

        teacherVideoMapper.updateById(teacherVideo);

        aliOssUtil.deleteFile(teacherUpdateVideoDto.getOldteacherVideoUrl());
    }

    @Override
    public List<DirectorGetworksVo> dirctorgetworks(DirectorGetWorksDto directorGetWorksDto) {
        LambdaQueryWrapper<TeacherVideo> o = new LambdaQueryWrapper<>();
        o.eq(directorGetWorksDto.getClassId()!=null,TeacherVideo::getClassId,directorGetWorksDto.getClassId());
        o.eq(directorGetWorksDto.getStatus()!=null,TeacherVideo::getStatus,directorGetWorksDto.getStatus());
        List<TeacherVideo> teacherVideos = teacherVideoMapper.selectList(o);
        List<DirectorGetworksVo> directorGetworksVos = new ArrayList<>();
        for (TeacherVideo t:teacherVideos) {
            Long teacherId = t.getTeacherId();
            Teacher teacher = teacherMapper.selectById(teacherId);

            Video video = videoMapper.selectById(t.getVideoId());
            if (video==null) {
                continue;
            }
            DirectorGetworksVo directorGetworksVo = new DirectorGetworksVo();

            directorGetworksVo.setId(t.getId());
            directorGetworksVo.setTeacherVideoUrl(t.getTeacherVideoUrl());
            directorGetworksVo.setDirctorResponse(t.getDirectorResponse());
            directorGetworksVo.setCreateTime(t.getCreateTime());
            directorGetworksVo.setTeachername(teacher.getName());
            directorGetworksVo.setVideoname(video.getVideoName());
            directorGetworksVo.setStatus(t.getStatus());
            directorGetworksVo.setOverrule(t.getOverrule());
            directorGetworksVos.add(directorGetworksVo);
        }
        return directorGetworksVos;
    }

    @Override
    @Transactional
    public String getAiresponse(AiDto aiDto) {

        TeacherVideo teacherVideo = new TeacherVideo();
        String post;
        try {
            post = this.post(aiProperties.getAiUrl(), aiDto.getImageUrl());

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        teacherVideo.setId(aiDto.getId());
        teacherVideo.setDirectorPictureUrl(aiDto.getImageUrl());
        teacherVideo.setAiResponse(post.toString());
        teacherVideoMapper.updateById(teacherVideo);
        return post.toString();
    }

    @Override
    public void putDirectorResponse(DirectorResponseDto dto) {
        //驳回状态不可批改
        TeacherVideo teacherVideo1 = teacherVideoMapper.selectById(dto.getId());
        if (teacherVideo1.getStatus()==2) {
            throw new RuntimeException(MessageConstant.RESPONSE_FAIL);
        }

        TeacherVideo teacherVideo = new TeacherVideo();
        teacherVideo.setId(dto.getId());
        teacherVideo.setDirectorResponse(dto.getResponse());
        //评论后将工作设置为已完成
        teacherVideo.setStatus(StatusConstant.AFTER_CORRECTION);
        teacherVideoMapper.updateById(teacherVideo);
    }

    @Override
    public void DirectorUpdateStatus(Long id,String overrule) {

        TeacherVideo teacherVideo = new TeacherVideo();
        TeacherVideo teacherVideo1 = teacherVideoMapper.selectById(id);
        //批改后不能驳回
        if (teacherVideo1.getStatus()==1) {
            throw new RuntimeException(MessageConstant.STATUS_FAIL);
        }
        teacherVideo.setId(id);
        teacherVideo.setStatus(StatusConstant.FAIL);
        teacherVideo.setOverrule(overrule);
        teacherVideoMapper.updateById(teacherVideo);
    }

    private String post(String postURL,String name) throws Exception {
        PostMethod postMethod = new PostMethod(postURL);
        postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        //参数设置，需要注意的就是里边不能传NULL，要传空字符串
        NameValuePair[] data = {
                new NameValuePair("image", name)
        };
        postMethod.setRequestBody(data);
        org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        System.out.println(result.toString());
        return result;

    }
}




